package com.seantone.inappupdate_flutter.inappupdate_flutter;

import android.app.Activity;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.seantone.inappupdate.core.InAppUpdate;
import com.seantone.inappupdate.core.define.CheckUpdateParams;
import com.seantone.inappupdate.core.define.CheckUpdateRet;
import com.seantone.inappupdate.core.impl.ICheckUpdateCallBack;
import com.seantone.inappupdate.core.impl.IRequestUpdate;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/** InappupdateFlutterPlugin */
public class InappupdateFlutterPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;
  private InAppUpdate mInAppUpdate = null;
  Activity mActivity = null;
  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "inappupdate_flutter");
    channel.setMethodCallHandler(this);
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    }else if (call.method.equals("checkUpdate")){
      mInAppUpdate.checkUpdate(mActivity, call.arguments().toString(), new ICheckUpdateCallBack() {
        @Override
        public void onRet(CheckUpdateRet ret) {
          result.success( new Gson().toJson(ret, CheckUpdateRet.class));
        }
      });
    }else if (call.method.equals("requestUpdate")){
      mInAppUpdate.requestUpdate(mActivity,  new IRequestUpdate() {
        @Override
        public void onProgress(int progress, int total) {

        }
      });
      result.success(null);
    }
    else {
      result.notImplemented();
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }

  @Override
  public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
    if(mInAppUpdate == null){
      mInAppUpdate = InAppUpdate.getInstance();
      mInAppUpdate.init( binding.getActivity());
    }
    mActivity = binding.getActivity();
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {
    mActivity = null;
  }

  @Override
  public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
    mActivity = binding.getActivity();
  }

  @Override
  public void onDetachedFromActivity() {
    mActivity = null;
  }
}
